package GUI.Productos;

import java.rmi.RemoteException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import article.entities.ArticleVO;
import article.exceptions.BadCategoryException;
import article.exceptions.IncompleteArticleException;
import article.exceptions.MissingArticleException;
import article.exceptions.MissingCategoryException;

import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import dbExceptions.DataBaseErrorException;
import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.article.interfaces.ArticleProxyMgt;

public class confirmAdd extends Shell {

	/**
	 * Launch the application.
	 * @param args
	 */
	public confirmAdd(ArticleVO art) {
		try {
			Display display = Display.getDefault();
			confirmAdd shell = new confirmAdd(display, art);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the shell.
	 * @param display
	 * @wbp.parser.constructor
	 */
	public confirmAdd(Display display, final ArticleVO art) {
		super(display, SWT.SHELL_TRIM);
		setLayout(new FormLayout());
		
		Label lblNombre = new Label(this, SWT.NONE);
		FormData fd_lblNombre = new FormData();
		fd_lblNombre.top = new FormAttachment(0, 70);
		fd_lblNombre.left = new FormAttachment(0, 45);
		lblNombre.setLayoutData(fd_lblNombre);
		lblNombre.setText("Nombre");
		
		Label lblPrecio = new Label(this, SWT.NONE);
		FormData fd_lblPrecio = new FormData();
		fd_lblPrecio.top = new FormAttachment(lblNombre, 25);
		fd_lblPrecio.left = new FormAttachment(lblNombre, 0, SWT.LEFT);
		lblPrecio.setLayoutData(fd_lblPrecio);
		lblPrecio.setText("Precio");
		
		Label lblCategoria = new Label(this, SWT.NONE);
		FormData fd_lblCategoria = new FormData();
		fd_lblCategoria.top = new FormAttachment(lblPrecio, 22);
		fd_lblCategoria.left = new FormAttachment(lblNombre, 0, SWT.LEFT);
		lblCategoria.setLayoutData(fd_lblCategoria);
		lblCategoria.setText("Categoria");
		
		Label lblIngredientes = new Label(this, SWT.NONE);
		FormData fd_lblIngredientes = new FormData();
		fd_lblIngredientes.top = new FormAttachment(lblCategoria, 25);
		fd_lblIngredientes.left = new FormAttachment(lblNombre, 0, SWT.LEFT);
		lblIngredientes.setLayoutData(fd_lblIngredientes);
		lblIngredientes.setText("Ingredientes");
		
		Label lblDescripcion = new Label(this, SWT.NONE);
		FormData fd_lblDescripcion = new FormData();
		fd_lblDescripcion.top = new FormAttachment(lblIngredientes, 25);
		fd_lblDescripcion.left = new FormAttachment(lblNombre, 0, SWT.LEFT);
		lblDescripcion.setLayoutData(fd_lblDescripcion);
		lblDescripcion.setText("Descripcion");
		
		Label nombre = new Label(this, SWT.NONE);
		FormData fd_nombre = new FormData();
		fd_nombre.bottom = new FormAttachment(lblNombre, 0, SWT.BOTTOM);
		fd_nombre.right = new FormAttachment(100, -140);
		nombre.setLayoutData(fd_nombre);
		nombre.setText(art.getName());
		
		Label precio = new Label(this, SWT.NONE);
		FormData fd_precio = new FormData();
		fd_precio.bottom = new FormAttachment(lblPrecio, 0, SWT.BOTTOM);
		fd_precio.left = new FormAttachment(nombre, 0, SWT.LEFT);
		precio.setLayoutData(fd_precio);
		precio.setText(String.valueOf(art.getPrice()));
		
		Label categoria = new Label(this, SWT.NONE);
		FormData fd_categoria = new FormData();
		fd_categoria.bottom = new FormAttachment(lblCategoria, 0, SWT.BOTTOM);
		fd_categoria.left = new FormAttachment(nombre, 0, SWT.LEFT);
		categoria.setLayoutData(fd_categoria);
		categoria.setText("New Label");
		
		Label ingredientes = new Label(this, SWT.NONE);
		FormData fd_ingredientes = new FormData();
		fd_ingredientes.bottom = new FormAttachment(lblIngredientes, 0, SWT.BOTTOM);
		fd_ingredientes.left = new FormAttachment(nombre, 0, SWT.LEFT);
		ingredientes.setLayoutData(fd_ingredientes);
		ingredientes.setText(art.getIngr()[0].getIngr());
		
		Label descripcion = new Label(this, SWT.NONE);
		FormData fd_descripcion = new FormData();
		fd_descripcion.bottom = new FormAttachment(lblDescripcion, 0, SWT.BOTTOM);
		fd_descripcion.left = new FormAttachment(nombre, 0, SWT.LEFT);
		descripcion.setLayoutData(fd_descripcion);
		descripcion.setText(art.getDesc());
		
		Label lblDatosNuevoProducto = new Label(this, SWT.NONE);
		FormData fd_lblDatosNuevoProducto = new FormData();
		fd_lblDatosNuevoProducto.top = new FormAttachment(0, 26);
		fd_lblDatosNuevoProducto.left = new FormAttachment(0, 151);
		lblDatosNuevoProducto.setLayoutData(fd_lblDatosNuevoProducto);
		lblDatosNuevoProducto.setText("DATOS NUEVO PRODUCTO");
		
		Composite composite = new Composite(this, SWT.NONE);
		FormData fd_composite = new FormData();
		fd_composite.right = new FormAttachment(0, 403);
		fd_composite.bottom = new FormAttachment(100, -10);
		fd_composite.left = new FormAttachment(0, 28);
		fd_composite.top = new FormAttachment(0, 261);
		composite.setLayoutData(fd_composite);
		
		Button btnAgregar = new Button(composite, SWT.NONE);
		btnAgregar.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				ArticleProxyMgt uso = ServiceFacade.getInstance().getArticleProxyMgt();
				try {
					uso.addArticle(art);
					MainProductos m = new MainProductos();
					
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IncompleteArticleException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ProxyException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (MissingCategoryException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (DataBaseErrorException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (BadCategoryException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (MissingArticleException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			dispose();
			}
		});
		btnAgregar.setBounds(24, 0, 94, 28);
		btnAgregar.setText("AGREGAR");
		
		Button btnNewButton = new Button(composite, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				dispose();
			}
		});
		btnNewButton.setBounds(220, 0, 94, 28);
		btnNewButton.setText("CANCELAR");
		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("CONFIRMACION");
		setSize(450, 325);

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

}
